base_url = "http://121.196.200.187:8899"
HOST = "121.196.200.187"
USER = 'root'
PWD = '123456'
DATABASE = 'sql121_196_200_'
import pymysql
import requests
import pytest
import allure
from faker import Faker
faker = Faker(locale='zh_cn')
class Pymysql:
    def __init__(self,user,pwd,host,database):
        self.user = user
        self.pwd = pwd
        self.host = host
        self.database = database
    def creat_connect(self):
        self.db = pymysql.connect(user=self.user,password=self.pwd,host=self.host,database='sql121_196_200_',cursorclass = pymysql.cursors.DictCursor)
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close()
        self.db.close()
    def select_all(self,sql):
        try:
            self.creat_connect()
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            self.close()
            return result
        except:
            print("%s 查询失败"%Exception)
    def insert(self,sql):
        try:
            self.creat_connect()
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
        except:
            print("%s插入失败"%Exception)
def insert_group(num):
    p1 = Pymysql(host=HOST, user=USER, pwd=PWD, database=DATABASE)

    result = []
    while True:
        if len(result) == num:
            break
        if faker.name() not in result:
            result.append(faker.name())
            p1.insert(f'insert into eb_user_group(group_name) values("{faker.name()}")')
p1 = Pymysql(host=HOST, user=USER, pwd=PWD, database=DATABASE)

@allure.feature("CRMEB")
class TestCRMEB:
    id_list = []
    @pytest.fixture(scope='function',autouse=True)
    def login(self):
        response = requests.post(base_url + '/adminapi/login',json =
        {"account":"admin","pwd":"Aa123456","key":"65654a0a3dcf0","captchaType":"blockPuzzle","captchaVerification":""})
        self.token = response.json()['data']['token']
        assert response.status_code == 200
        assert response.json()['msg'] == 'success'

    def test_add_group(self):
        # 死磕

        name = faker.name()
        result = p1.select_all('select * from eb_user_group')
        for i in result:
            if name == i['group_name']:
                print("插入的分组存在")
        res = requests.post(base_url+"/adminapi/user/user_group/save.html",{"group_name":name},headers={"Authori-zation":"Bearer "+str(self.token)})
        assert res.status_code == 200
        assert res.json()['msg'] == '提交成功'
        result = p1.select_all('select count(*) from eb_user_group')
        # print(result[0]['count(*)']) # 104
        # print(104/15+1) # 6.9 + 1 = 7.9
        for i in range(1,int(result[0]['count(*)']/15) + 2): # 7.9 会走到7吗？ 走到6   7.9+1  会走到7吗？
            response = requests.get(base_url + f"/adminapi/user/user_group/list?page={i}&limit=15",
                         headers={"Authori-zation": "Bearer " + str(self.token)})
            for i in  response.json()['data']['list']:
                if i['group_name'] == name:
                    print(i['id'])
                    self.id_list.append(i['id'])
        for i in range(1, int(6.9) + 2):  # 6
            print(i)
        # 优雅 经典
        name = faker.name()
        #
        res = requests.post(base_url + "/adminapi/user/user_group/save.html", {"group_name": name},
                            headers={"Authori-zation": "Bearer " + str(self.token)})
        assert res.status_code == 200
        assert res.json()['msg'] == '提交成功'
        result = p1.select_all(f'select id from eb_user_group where group_name="{name}"')
        # print(result[0]['id'])
        self.id_list.append(result[0]['id'])

    def test_delete_group(self):
        res = requests.delete(base_url + "/adminapi/user/user_group/del/" + str(self.id_list[0]),headers={"Authori-zation": "Bearer " + str(self.token)})
        assert res.json()['msg'] == "删除成功!"
        assert res.status_code == 200
for i in range(1,5):
    print(i)
